package org.particles.domain.field;

import org.particles.domain.Particle;
import org.particles.domain.Vector;

public class StringForce implements Field {
	private long fieldMarker;
	/** min distance between particle boundary plus this values. */
	private double maxLength = 18;
	/** distance between particle boundary */
	private double minLength = 2;
	private double attrStrength = 0.5;
	private double rejStrength = 1;
	

	public StringForce(long fieldMarker) {
		this.fieldMarker = fieldMarker;
	}

	@Override
	public void applyForces(Particle p1, Particle p2) {
		final double distance = p1.distanceTo(p2);
		final double futureDistance = p2.getSpacePlace().addTo(p2.getImpulse())
				.distanceTo(p1.getSpacePlace());
		final double minLength = p1.getRadius() + p2.getRadius() + this.minLength;
		if(distance < minLength && futureDistance < minLength) {
			// rejection
			final Vector forceVector = p1.getSpacePlace().subtractFrom(p2.getSpacePlace());
			forceVector.setLength(rejStrength);
			p2.getImpulse().add(forceVector);
		}
		else if(distance < minLength + maxLength 
				&& futureDistance < minLength + maxLength) {
			// attraction
			final Vector forceVector = p2.getSpacePlace().subtractFrom(p1.getSpacePlace());
			forceVector.setLength(attrStrength);
			p2.getImpulse().add(forceVector);
		}
	}

	public double getMaxLength() {
		return maxLength;
	}

	public void setMaxLength(double maxLength) {
		this.maxLength = maxLength;
	}

	public double getMinLength() {
		return minLength;
	}

	public void setMinLength(double minLength) {
		this.minLength = minLength;
	}

	public double getAttrStrength() {
		return attrStrength;
	}

	public void setAttrStrength(double attrStrength) {
		this.attrStrength = attrStrength;
	}

	public double getRejStrength() {
		return rejStrength;
	}

	public void setRejStrength(double rejStrength) {
		this.rejStrength = rejStrength;
	}

}
